package com.wushi.lenovo.asia5b.app_di.goods.presenter;

import android.text.TextUtils;

import com.mvp.base.BasePresenter;
import com.mvp.base.NetParams;
import com.mvp.common.GlobalValue;
import com.mvp.common.LocalManageUtil;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_di.goods.mvpview.OrderItemFragmentView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_http.OrderService;
import com.wushi.lenovo.asia5b.app_model.good.orderitem.OrderItemModel;
import com.wushi.lenovo.asia5b.app_model.good.orderitem.OrderItemType;
import com.wushi.lenovo.asia5b.app_model.good.orderitem.SOrderItemModel;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;


/**
 * Created by zhangyuncai on 2019/1/23.
 */
public class OrderItemFragmentPresenter extends BasePresenter<OrderItemFragmentView> {

    ApiService apiService;
    OrderService orderService;
    protected int page;//当前页数
    protected int sumPage;//最后页数

    @Inject
    public OrderItemFragmentPresenter(ApiService apiService, OrderService orderService) {
        this.apiService = apiService;
        this.orderService = orderService;
    }


    public void getOrders(OrderItemType type, int refresh) {
        if (!UserRepository.isLogin()) {
            return;
        }
        if (refresh <= 1) {
            page = 1;
            sumPage = 1000;
        } else {
            page++;
        }
        if (page > sumPage) {
            getView().dismissLoadingBar("");
            return;
        }
        Map<String, Object> map = MapHelper.newHashMap();
        map.put("member_id", UserInfo.getMemberId());
        map.put("language_code", LocalManageUtil.getAppLanguage());
        map.put("length", GlobalValue.records);
        map.put("page_no", page);


        baseSubscribe(apiService.getOrders(type.getOrderListPath(), map),
                new NetParams(false),
                sOrderItemModelResultModel -> {
                    int total=0;
                    if (sOrderItemModelResultModel.isSuccess()) {
                        List<OrderItemModel> orderItemModelList = new ArrayList<>();
                        SOrderItemModel sOrderItemModel = sOrderItemModelResultModel.getData();
                        if (sOrderItemModel != null && sOrderItemModel.getData() != null) {
                            orderItemModelList.addAll(sOrderItemModel.getData());
                            sumPage = GlobalValue.getSumPage(GlobalValue.records, sOrderItemModel.getTotal());
                            if (UserRepository.isLogin()) {//无人登录的情况下仍然显示数据
                                total=sOrderItemModel.getTotal();
                            }
                        }
                        if (UserRepository.isLogin()) {//无人登录的情况下仍然显示数据
                            getView().getOrdersResult(orderItemModelList, page, sumPage);
                        }
                    }
                    getView().countResult(total);
                });


    }

    /**
     * @param map
     * @param suffixPath 类似orderdg.detail.received
     */
    public void button_order(Map<String, Object> map, String suffixPath) {
        if (!UserRepository.isLogin() || TextUtils.isEmpty(suffixPath)) {
            return;
        }
        baseSubscribe(orderService.button_order(suffixPath, map),
                resultModel -> {
                    if (UserRepository.isLogin()) {//无人登录的情况下仍然显示数据
                        getView().button_orderResult(resultModel, map);
                    }
                });
    }

}